//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.ComponentModel;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
	///<summary>
	/// Represents the filter column
	///</summary>
	public class FilterColumn: IOfficeXmlSerializable, INotifyPropertyChanged
	{
	   internal FilterColumn(AutoFilter autoFilter, Range r)
		{
			_autoFilter = autoFilter;
			_range = r;
		}
		internal FilterColumn(AutoFilter autoFilter, Range range, Column column)
			: this (autoFilter, range)
		{
			_column = column;
		}
		private readonly AutoFilter _autoFilter;
		internal AutoFilter AutoFilter
		{
			get { return _autoFilter; }
		}
	
		private readonly Range _range;
		public Range Range
		{
			get { return _range; }
		}
	
		private Column _column;
		public Column Column
		{
			get { return _column; }
			internal set { _column = value; }
		}
	
		static Filter ReadFilter(ReadContext context)
		{
			Filter result;
				switch (context.ReaderLocalName)
				{
					case null:
						return null;
					case "customFilters":
						result = new CustomFilters(); break;
					case "filters": result = new ValuesFilter(); break;
					case "top10":
						result = new Top10Filter(); break;
					case "dynamicFilter":
						result = new DynamicFilter(); break;
					case "colorFilter":
						result = new ColorFilter(); break;
					case "iconFilter":
						result = new IconFilter(); break;
					case "extLst":
						throw new NotImplementedException();
					default: throw new NotSupportedException();
				}
				IOfficeXmlSerializable s = result;
			s.ReadXml(context);
			
			return result;
		}

	   //\\ internal abstract void ReadXmlCore(ReadContext context);
		#region IOfficeXmlSerializable Members

		void IOfficeXmlSerializable.ReadXml(ReadContext context)
		{
			using (ReadContext c = context.Read("filterColumn"))
			{
				int ColumnId = c.GetInteger("colId");
				_column = Range.Worksheet.Columns[Range.TopLeftCell.Column.Index + ColumnId];
				HiddenButton = c.GetOptionalBoolean("hiddenButton", false);
				ShowButton = c.GetOptionalBoolean("showButton", true);
				Filter = ReadFilter(c);
//                Item = FilterColumnItem.ReadFilterColumnItem(c);
			}

		}
		void IOfficeXmlSerializable.WriteXml(WriteContext context)
		{
			if (Filter == null)
				return;
			using (WriteContext c = context.Write("filterColumn"))
			{
				c.SetInteger("colId", Column.Index - Range.TopLeftCell.Column.Index);
				c.SetOptionalBoolean("hiddenButton", HiddenButton, false);
				c.SetOptionalBoolean("showButton", ShowButton, true);
				c.WriteOptionalElement(Filter);
			}
		}

		//internal abstract void WriteXmlCore(WriteContext context);

		#endregion

		private Filter _filter;

		public Filter Filter
		{
			get { return _filter; }
			set
			{
				_filter = value;
				AutoFilter.Apply();
				NotifpPropertyChanged("Filter");
			}
		}


		private bool _showButton = true;

		public bool ShowButton
		{
			get { return _showButton; }
			set { _showButton = value; NotifpPropertyChanged("ShowButton"); }
		}
	
		private bool _hiddenButton;

		public bool HiddenButton
		{
			get { return _hiddenButton; }
			set { _hiddenButton = value; NotifpPropertyChanged("HiddenButton"); }
		}
	
	   /* private int _colId;

		public int ColumnId
		{
			get { return _colId; }
			set { _colId = value; }
		}*/





		void NotifpPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		#endregion

		internal Range RangeWithoutHeader
		{
			get
			{
				Cell c1 = Range.TopLeftCell;
				return new MultiCellsRange(Range.Worksheet[c1.Row.Index + 1, c1.Column.Index],
					Range.Worksheet[c1.Row.Index + Range.RowSpan - 1 , c1.Column.Index + Range.ColumnSpan - 1]);

			}
		}

		internal Func<Cell, bool> GetCellEvaluator(Range rangeWithoutHeader)
		{
			if (Filter == null)
				return c => true;
			return Filter.GetCellEvaluator(rangeWithoutHeader);
		}
	}
}
